//
//  VisitorView.m
//  weibo
//
//  Created by hp on 16/4/22.
//  Copyright © 2016年 hp. All rights reserved.
//

#import "VisitorView.h"
// 省略mas_开头
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry.h>
@interface VisitorView ()
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIImageView *homeIcon;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UIImageView *maskBGView;
@end

@implementation VisitorView

- (void)setupVisitorInfo:(BOOL)isHome imageName:(NSString *)imageName message:(NSString *)message
{
    self.iconView.hidden = !isHome;
    self.homeIcon.image = [UIImage imageNamed:imageName];
    self.messageLabel.text = message;
    
    if (isHome) {
        [self startAnimation];
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self addSubview:self.iconView];
        [self addSubview:self.maskBGView];
        [self addSubview:self.homeIcon];
        [self addSubview:self.messageLabel];
        [self addSubview:self.loginBtn];
        [self addSubview:self.registerBtn];
        
        [self.iconView makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.center);
            
        }];
        
        [self.homeIcon makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.center);
        }];
        
        [self.maskBGView makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(self);
        }];
        
        [self.messageLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconView.bottom);
            make.centerX.equalTo(self.centerX);
            make.width.equalTo(self.width).multipliedBy(0.8);
        }];
        
        [self.loginBtn makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.messageLabel.bottom);
            make.right.equalTo(self.centerX).offset(-10);
            make.width.equalTo(@100);
            
        }];
        
        [self.registerBtn makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.messageLabel.bottom);
            make.left.equalTo(self.centerX).offset(10);
            make.width.equalTo(@100);
        }];
    }
    return self;
}

- (void)startAnimation
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = @(2 * M_PI);
    anim.duration = 20;
    anim.repeatCount = MAXFLOAT;
    anim.removedOnCompletion = NO;
    
    [self.iconView.layer addAnimation:anim forKey:@""];
}

#pragma mark - 控件懒加载
- (UIImageView *)maskBGView
{
    if (_maskBGView==nil) {
        _maskBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _maskBGView;
}

- (UIImageView *)iconView
{
    if (_iconView==nil) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    return _iconView;
}

- (UIImageView *)homeIcon
{
    if (_homeIcon==nil) {
        _homeIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    return _homeIcon;
}

- (UILabel *)messageLabel
{
    if (_messageLabel==nil) {
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.numberOfLines = 0;
        _messageLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _messageLabel;
}


- (UIButton *)registerBtn
{
    if (_registerBtn==nil) {
        _registerBtn = [[UIButton alloc] init];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_registerBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registerBtn addTarget:self action:@selector(registerBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

- (UIButton *)loginBtn
{
    if (_loginBtn==nil) {
        _loginBtn = [[UIButton alloc] init];
        [_loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

#pragma mark - 控件事件处理
- (void)loginBtnPressed
{
    [self.delegate loginBtnWillPressed];
}

- (void)registerBtnPressed
{
    [self.delegate registerBtnWillPressed];
}


@end
